﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.Devices.Sensors;
using Windows.UI.Xaml.Media;

namespace Level
{
    partial class MainPage
    {
        private Windows.Devices.Sensors.Inclinometer _inclinometer;
        private Exception _lastException;
        private double? _lastReading;

        public MainPage()
        {
            InitializeComponent();
            return;
            _inclinometer = Inclinometer.GetDefault();
            if (_inclinometer == null)
            {
                Angle.Text = "Could not get angle (no inclinmoeter)";
            }
            else
            {
                _inclinometer.ReportInterval = Math.Max(100, _inclinometer.MinimumReportInterval);
                _inclinometer.ReadingChanged += new TypedEventHandler<Inclinometer, InclinometerReadingChangedEventArgs>(_inclinometer_ReadingChanged);
            }

            //var transform = new CompositeTransform();
            //transform.TranslateX = 10;
            //Bubble.RenderTransform = transform;
        }

        void _inclinometer_ReadingChanged(Inclinometer sender, InclinometerReadingChangedEventArgs args)
        {
            _lastReading = args.Reading.RollDegrees;
            this.Dispatcher.Invoke(Windows.UI.Core.CoreDispatcherPriority.Normal, updateScreen, sender, null);
        }

        private void updateScreen(object sender, Windows.UI.Core.InvokedHandlerArgs e)
        {
            if (_lastReading == null)
            {
                Angle.Text = "Could not get angle (no value)";
                return;
            }
            Angle.Text = _lastReading.Value.ToString("0.#");
        }
    }
}
